package com.example.thread.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.mybatisplusstudy.mapper.BatchNoMapper;
import com.example.mybatisplusstudy.mapper.DataMapper;
import com.example.mybatisplusstudy.mapper.LockConfigMapper;
import com.example.mybatisplusstudy.pojo.BatchNoDto;
import com.example.mybatisplusstudy.pojo.LockDto;
import com.example.thread.LockService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.UUID;

/**
 * @author code威
 * @version 1.0
 * @description: 锁服务
 * @date 2022/7/23 12:34
 */
@Service
public class LockServiceImpl implements LockService {

    @Autowired
    private LockConfigMapper lockConfigMapper;

    @Autowired
    private DataMapper dataMapper;

    @Autowired
    private BatchNoMapper batchNoMapper;


    @Override
    public boolean getLock(String lockName,String lockOwner,String lockStatus) {


        int lock = lockConfigMapper.getLock(lockName, lockOwner, lockStatus);
        if(lock > 0){
            return true;
        }

        return false;
    }

    @Override
    public void updateLockStatus(String lockName, String lockOwner, String lockStatus) {
        lockConfigMapper.update(null,new LambdaUpdateWrapper<>(LockDto.class).set(LockDto::getLockStatus,lockStatus)
                .eq(LockDto::getLockOwner,lockOwner).eq(LockDto::getLockName,lockName));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void handleData() throws Exception {

        long beginTime = System.currentTimeMillis();
        for (int i = 0;i <= 100;i++){
            String batchNo = "TEST04"+ UUID.randomUUID().toString();

//            if(i == 30){
//                throw new Exception("测试异常");
//            }
            BatchNoDto batchNoDto = new BatchNoDto();
            batchNoDto.setBatchNo(batchNo);
            dataMapper.updateData(batchNo);
            batchNoMapper.insert(batchNoDto);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("==消耗时间=="+(endTime-beginTime)+"ms");



    }
}
